package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@SuppressWarnings("unused")
public class MysqlUtils {

	public static String driver = "com.mysql.jdbc.Driver";

	public static String url = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";

	public static String user = "root";

	public static String password = "pf@147258";

	public static void main(String[] args) {

		long start = System.currentTimeMillis();

		Connection con;

		String prefix = "INSERT INTO `t_test` VALUES ";
		StringBuffer suffix = new StringBuffer();

		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, user, password);
			if (!con.isClosed()) {
				System.out.println("Succeeded connecting to the Database!");
			} else {
				System.out.println("Failed connecting to the Database!");
				throw new RuntimeException();
			}
			con.setAutoCommit(false);

			PreparedStatement pst = con.prepareStatement("VERSION();");
			pst.clearBatch();

			int num = 0;

			// todo 设置循环
			for (int i = 0; i < 100; i++) {

				// todo 设置循环
				suffix.append("~~~~~~~~~");

				// 构建完整sql
				String sql = prefix + suffix.substring(0, suffix.length() - 1);

				// System.out.println(sql);

				// 添加执行sql
				pst.addBatch(sql);
				// 执行操作
				pst.executeBatch();
				// 提交事务
				con.commit();

				// 清空
				pst.clearBatch();
				suffix = new StringBuffer();
			}

			pst.close();
			con.close();
		} catch (Exception e) {
			System.out.println(suffix);
			e.printStackTrace();
		}

		System.out.println("耗时：" + (System.currentTimeMillis() - start) / 1000);
	}
}
